package com.ytzl.itrip.biz.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.ytzl.itrip.beans.model.ItripUserLinkUser;
import com.ytzl.itrip.beans.model.vo.ItripUserLinkUserVo;
import com.ytzl.itrip.biz.service.ItripUserLinkUserService;
import com.ytzl.itrip.utils.common.Dto;
import com.ytzl.itrip.utils.common.DtoUtil;
import com.ytzl.itrip.utils.common.RedisAPI;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by asus on 2018/7/18.
 */
@Controller
@RequestMapping("/api/userinfo")
public class UserinfoController {

    @Resource
    private RedisAPI redisAPI;
    @Resource
    private ItripUserLinkUserService itripUserLinkUserService;

    @RequestMapping(value = "/queryuserlinkuser",method = RequestMethod.POST)
    @ResponseBody
    public Dto queryuserlinkinfo(@RequestBody JSONObject json,
                                 HttpServletRequest request){
        String token = request.getHeader("token");
        Map map = new HashMap();
        map.put("linkUserName",json.getString("linkUserName"));
        map.put("createdBy",token.split("_")[3]);
        List<ItripUserLinkUserVo> itripUserLinkUserVos = new ArrayList<>();
        try {
            List<ItripUserLinkUser> itripUserLinkUsers = itripUserLinkUserService.getListByMap(map);
            if (itripUserLinkUsers!=null){
                for (ItripUserLinkUser itripUserLinkUser:itripUserLinkUsers){
                    ItripUserLinkUserVo itripUserLinkUserVo = new ItripUserLinkUserVo();
                    BeanUtils.copyProperties(itripUserLinkUser,itripUserLinkUserVo);
                    itripUserLinkUserVos.add(itripUserLinkUserVo);
                }
                return DtoUtil.returnSuccess("查询成功",itripUserLinkUserVos);
            }else{
                return DtoUtil.returnFail("","");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnFail("获取常用联系人失败","100401");
    }

    @RequestMapping(value = "/adduserlinkuser",method = RequestMethod.POST)
    @ResponseBody
    public Dto savesuerinfo(@RequestBody JSONObject json,HttpServletRequest request){
        if (json!=null){
            String userid = request.getHeader("token").split("_")[3];
            ItripUserLinkUser itripUserLinkUser = new ItripUserLinkUser();
            itripUserLinkUser.setLinkIdCard(json.getString("linkIdCard"));
            itripUserLinkUser.setLinkPhone(json.getString("linkPhone"));
            itripUserLinkUser.setLinkUserName(json.getString("linkUserName"));
            itripUserLinkUser.setCreatedBy(Long.parseLong(userid));
            itripUserLinkUser.setLinkIdCardType(0);
            try {
                itripUserLinkUserService.save(itripUserLinkUser);
                return DtoUtil.returnSuccess("新增成功",itripUserLinkUser);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("新增联系人失败","100411");
            }
        }else{
            return DtoUtil.returnFail("不能提交空，请填写常用联系人信息","100412");
        }
    }

    @RequestMapping(value = "/modifyuserlinkuser",method = RequestMethod.POST)
    @ResponseBody
    public Dto modifyuserinfo(@RequestBody JSONObject json){
        if (json!=null){
            ItripUserLinkUser itripUserLinkUser = new ItripUserLinkUser();
            itripUserLinkUser.setLinkIdCard(json.getString("linkIdCard"));
            itripUserLinkUser.setLinkPhone(json.getString("linkPhone"));
            itripUserLinkUser.setLinkUserName(json.getString("linkUserName"));
            itripUserLinkUser.setId(Long.parseLong(json.getString("id")));
            try {
                itripUserLinkUserService.modify(itripUserLinkUser);
                return DtoUtil.returnSuccess("修改成功",itripUserLinkUser);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("修改失败","100421");
            }
        }else{
            return DtoUtil.returnFail("不能提交空，请填写常用联系人信息","100422");
        }
    }

    @RequestMapping(value = "/deluserlinkuser",method = RequestMethod.GET)
    @ResponseBody
    public Dto deluserinfo(@RequestParam("ids") Long[] ids){
        if (ids!=null){
            try {
                Integer i = itripUserLinkUserService.removeById(ids);
                if (i>0){
                    return DtoUtil.returnSuccess("remove success");
                }else{
                    return DtoUtil.returnFail("删除失败，有订单未处理","100431");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("Failed Remove","100432");
            }
        }else{
            return DtoUtil.returnFail("请选择要删除的联系人","100433");
        }
    }
}
